package com.aaa.service.impl;

import com.aaa.dao.CheckItemDao;
import com.aaa.entity.CheckItem;
import com.aaa.service.CheckItemService;
import com.aaa.util.AccountName;
import com.aaa.vo.CheckItemVo;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @BelongsPackage: com.aaa.service.impl
 * @Description:
 * @CreateTime: 2023-03-30  16:15
 * @Author: ql
 */
@Service
public class CheckItemServiceImpl implements CheckItemService {
    @Autowired
    private CheckItemDao checkItemDao;
    @Override
    public Result<IPage<CheckItem>> queryPage(CheckItemVo checkItemVo) {
        Page<CheckItem> checkItemPage = new Page<>(checkItemVo.getPageNum(),checkItemVo.getPageSize());
        QueryWrapper<CheckItem> wrapper = new QueryWrapper<>();
        if (StringUtils.hasText(checkItemVo.getCheckItemName())){
            wrapper.like("check_item_name",checkItemVo.getCheckItemName());
        }if (StringUtils.hasText(checkItemVo.getKeywords())){
            wrapper.like("keywords",checkItemVo.getKeywords());
        }if (StringUtils.hasText(checkItemVo.getTypeId())){
            wrapper.like("type_id",checkItemVo.getTypeId());
        }if (StringUtils.hasText(checkItemVo.getStatus())){
            wrapper.like("status",checkItemVo.getStatus());
        }
        IPage<CheckItem> checkItemPage1 = checkItemDao.selectPage(checkItemPage, wrapper);
        return new Result<>(2000,"ok",checkItemPage1);
    }

    @Override
    public Result<CheckItem> queryById(Long id) {
        QueryWrapper<CheckItem> wrapper = new QueryWrapper<>();
        wrapper.eq("status",0);
        wrapper.eq("check_item_id",id);
        CheckItem checkItem = checkItemDao.selectOne(wrapper);
        return new Result<>(2000,"ok",checkItem);
    }
    @Autowired
    AccountName accountName;

    @Override
    public Result<String> addOrUpdCheckItem(CheckItem checkItem) {
        String userName = accountName.getAccountName();
        System.out.println("添加或修改的====================== " + checkItem);
        int num;
        if (checkItem.getCheckItemId()!=null){
            checkItem.setUpdateBy(userName);
            checkItem.setUpdateTime(new Date());
            //修改
            num = checkItemDao.updateById(checkItem);
        }else {
            checkItem.setCreateBy(userName);
            checkItem.setCreateTime(new Date());
            num = checkItemDao.insert(checkItem);
        }
        if (num>0){
            return Result.success();
        }
        return Result.error();

    }

    @Override
    public Result<String> deleteByIds(Long[] id) {
        List<Long> collect1 = Arrays.stream(id).collect(Collectors.toList());
        System.out.println("===========多选删除id==============="+collect1);
        int i = checkItemDao.deleteBatchIds(collect1);
        if (i>0){
            return Result.success();
        }
        return Result.error();
    }

    @Override
    public List<CheckItem> queryList() {
        QueryWrapper<CheckItem> wrapper = new QueryWrapper<>();
        wrapper.eq("status",0);
        return checkItemDao.selectList(wrapper);
    }
}
